/**
 * Created by ztt on 2018/12/3.
 */
import {Observable, Observer, observable} from 'rxjs';
var foo = Observable.create(function (observer:any) {
    console.log('Hello');
    observer.next(42);
});

foo.subscribe(function (x:number) {
    console.log('foo：',x);
});
foo.subscribe(function (y:number) {
    console.log("foo：",y);
});
/**
 * todo:函数不能多次return
 * observable可以返回多次
 */

var foo2=Observable.create( (observer:any)=>{
    observer.next(1);
    observer.next(12);
});
console.log('- - - - before(测试同步异步)- - - - ');
foo2.subscribe( (x:number)=>{
    console.log("foo2：",x);
});
foo2.subscribe( (x:number)=>{
    console.log("foo2：",x);
});
console.log('- - - - after(测试同步异步)- - - - ');

/**todo:下面的示例创建了一个 Observable，它每隔一秒会向观察者发送字符串 'hi' 。
 **/
var foo3=Observable.create((observer:any)=>{
    var id = setInterval(() => {
        observer.next('hi')
    }, 1000);
});
var subscribe=foo3.subscribe((x:string) => console.log("foo3：",x));
setTimeout( ()=>{
    subscribe.unsubscribe();
},5000);
/** todo:Observable可以传递3种类型的值
 * Next:通知，发送一个值，比如数字、字符串、对象等
 * Error：通知，发送一个javascript错误或异常
 * Complete:通知，不再发送任何值
 **/
var foo4=Observable.create((observer:any)=>{
    try {
        observer.next(1);
        observer.next(2);
        observer.next(3);
        observer.complete();
    } catch (err) {
        observer.error(err); // 如果捕获到异常会发送一个错误
    }
});
foo4.subscribe()
/** todo：Observer(观察者)
 */

var observer04={
    next: (x:any)=> console.log('observer04->x：',x),
    error:( err:any)=>console.log('observer04->err:',err),
    complete:()=>console.log('observer04->complete')
}
foo4.subscribe(observer04);